<template>
  <el-row>
    <el-col :span="24">
      <Portlet title="交易管理">
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">
            <el-col :lg="4" :md="6">
              <el-form-item prop="orderNo">
                <el-input v-model="search.orderNo" type="textarea" :rows="4" placeholder="订单编号"
                          clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="mallProductNo">
                <el-input v-model="search.mallProductNo" placeholder="商品编号" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="productName">
                <el-input v-model="search.productName" placeholder="商品名称" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" native-type="submit" :loading="searchLoading" plain @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="primary" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="productNo">
                <el-input v-model="search.productNo" placeholder="厂家产品编号" clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="startTime">
                <el-date-picker type="date"
                                placeholder="下单开始时间"
                                v-model="search.startTime"
                                value-format="yyyy-MM-dd"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" native-type="submit" :loading="searchLoading" plain @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="primary" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="endTime">
                <el-date-picker type="date"
                                placeholder="下单结束时间"
                                v-model="search.endTime"
                                value-format="yyyy-MM-dd"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </Portlet>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-warehouse-store/order/get-dealManage-list"
        sequence
        :columns="columns">
        <template slot="preColumn">
          <el-table-column prop="orderNo" width="100px" align="center" label="订单编号"></el-table-column>
        </template>

        <template slot="column">
          <el-table-column fixed="right" align="center" width="140px" label="操作">
            <template slot-scope="scope">
              <el-button
                v-if="showMakeSure(scope.row)"
                size="mini"
                type="text"
                @click.prevent="makeSure(scope.row)"
              >
                确认
              </el-button>

              <el-button
                v-if="showDelivery(scope.row)"
                size="mini"
                @click.prevent="toDelivery(scope.row)"
                type="text"
              >
                发货
              </el-button>

              <el-button
                v-if="showClose(scope.row)"
                size="mini"
                type="text"
                @click.prevent="close(scope.row)"
              >
                关闭
              </el-button>

              <el-button
                size="mini"
                type="text"
                @click.prevent="toGoods(scope.row)"
              >
                查看商品
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import {decimalNumFilter, unixTimestampFilter, filterDataBaseDictionary} from '@/filters'
import {copy, confirm} from '@/utils'

export default {
  name: 'shopStore',
  filters: {
    decimalNumFilter,
    filterDataBaseDictionary
  },
  data() {
    return {
      searchLoading: false,
      search: {
        orderNo: '',
        mallProductNo: '',
        productName: '',
        productNo: '',
        startTime: '',
        endTime: ''
      },
      isOpen: false,
      columns: [
        {
          label: '商品编号',
          data: 'mallProductNo'
        },
        {
          label: '厂家产品编号',
          data: 'productNo'
        },
        {
          label: '商品名称',
          minWidth: '200px',
          data: 'productName'
        },
        {
          label: '数量',
          data: 'productNum'
        },
        {
          label: '交易金额',
          data: 'totalMoney'
        },
        {
          label: '客户姓名',
          data: 'consigneName'
        },
        {
          label: '客户电话',
          data: 'consignePhone'
        },
        {
          label: '客户地址',
          data: 'province+city+district+address',
          minWidth: '200px',
          render: (data, full) => {
            return `${full.consigneProvince} ${full.consigneCity} ${full.consigneDistrict} ${full.consigneAddress}`
          }
        },
        {
          label: '下单时间',
          data: 'foundtime',
          render(data, full) {
            return unixTimestampFilter(data)
          }
        },
        {
          label: '订单状态',
          data: 'orderState',
          render(data, full) {
            return filterDataBaseDictionary(data)
          }
        }
      ]
    }
  },
  methods: {
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    // 获取图片数组
    getImageArr(row) {
      const shopLogo = row.shopLogo
      if (shopLogo && typeof shopLogo === 'string') {
        return shopLogo.split(',')
      }
      return []
    },
    // 展示关闭按钮
    showClose(row) {
      const orderState = row.orderState
      // mall-100：待支付 mall-200：待确认 mall-210：待发货
      if (['mall-100', 'mall-200', 'mall-210'].indexOf(orderState) >= 0) {
        return true
      }
      return false
    },
    close(row) {
      let data = {mallOrderId: row.mallOrderId}
      confirm('确认关闭此订单', '提示', {
        confirmButtonLoading: true,
        dangerouslyUseHTMLString: true,
        showCancelButton: true,
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          this.$ajax.post('/ms-warehouse-store/order/cancel-order', data).then(response => {
            if (response.success) {
              close().then(_ => {
                confirm('操作成功')
              })
              this.refresh()
            }
            confirmButtonLoadingClose()
          }).catch(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    },
    // 展示确认按钮
    showMakeSure(row) {
      const orderState = row.orderState
      // mall-200：待确认
      if (['mall-200'].indexOf(orderState) >= 0) {
        return true
      }
      return false
    },
    makeSure(row) {
      let data = {mallOrderId: row.mallOrderId}
      confirm('确认该订单可做？', '提示', {
        confirmButtonLoading: true,
        dangerouslyUseHTMLString: true,
        showCancelButton: true,
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          this.$ajax.post('/ms-warehouse-store/order/notarize-order', data).then(response => {
            if (response.success) {
              close().then(_ => {
                confirm('操作成功')
              })
              this.refresh()
            }
            confirmButtonLoadingClose()
          }).catch(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    },
    // 展示发货
    showDelivery(row) {
      const orderState = row.orderState
      // mall-210：待发货
      if (['mall-210'].indexOf(orderState) >= 0) {
        return true
      }
      return false
    },
    // 去下单
    toDelivery(row) {
      this.$router.push({path: '/redirect/ss-oms/shop/delivery/' + row.mallOrderId})
    },
    toGoods(data) {
      this.$router.push({path: '/redirect/ss-oms/shop/goods', query: {mallProductNo: data.mallProductNo}})
    }
  }
}
</script>
